package gui_classes;

import java_persistence.ServicesMapper;

import javax.swing.JPanel;
import javax.swing.JLabel;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JButton;

import domain.Invoice;
import domain.Services;

/*
 * This class represents the Invoice Screen
 */
public class InvoiceScreen extends DERPBase {
	private JTextField txtService;
	private JTextField txtService_1;
	private JTextField txtService_2;
	private JTextField txtService_3;
	private JTextField txtService_4;
	private JTextField txtCost;
	private JTextField txtCost_1;
	private JTextField txtCost_2;
	private JTextField txtCost_3;
	private JTextField txtCost_4;

	/**
	 * Create the panel.
	 */
	public InvoiceScreen() {
		setLayout(null);
		setPreferredSize(new Dimension(450,300));
		
		JLabel lblPatientInvoice = new JLabel("Patient Invoice");
		lblPatientInvoice.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblPatientInvoice.setBounds(158, 11, 129, 14);
		add(lblPatientInvoice);
		
		JLabel lblPatientName = new JLabel("Patient Name");
		lblPatientName.setFont(new Font("Tahoma", Font.BOLD, 11));
		lblPatientName.setBounds(10, 39, 108, 14);
		add(lblPatientName);
		
		JButton btnAdd = new JButton("Add");
		btnAdd.setBounds(158, 263, 117, 25);
		btnAdd.addActionListener(new AddListener());
		add(btnAdd);
		
		JLabel label = new JLabel("");
		label.setBounds(10, 120, 70, 15);
		add(label);
		
		JLabel lblServices = new JLabel("Services");
		lblServices.setBounds(20, 65, 70, 15);
		add(lblServices);
		
		JLabel lblCost = new JLabel("Cost($)");
		lblCost.setBounds(294, 65, 70, 15);
		add(lblCost);
		
		txtService = new JTextField();
//		txtService.setText("Service1");
		txtService.setBounds(12, 92, 114, 19);
		add(txtService);
		txtService.setColumns(10);
		
		txtService_1 = new JTextField();
//		txtService_1.setText("Service2");
		txtService_1.setBounds(10, 118, 114, 19);
		add(txtService_1);
		txtService_1.setColumns(10);
		
		txtService_2 = new JTextField();
//		txtService_2.setText("Service3");
		txtService_2.setBounds(10, 147, 114, 19);
		add(txtService_2);
		txtService_2.setColumns(10);
		
		txtService_3 = new JTextField();
//		txtService_3.setText("Service4");
		txtService_3.setBounds(12, 178, 114, 19);
		add(txtService_3);
		txtService_3.setColumns(10);
		
		txtService_4 = new JTextField();
//		txtService_4.setText("Service5");
		txtService_4.setBounds(10, 209, 114, 19);
		add(txtService_4);
		txtService_4.setColumns(10);
		
		txtCost = new JTextField();
//		txtCost.setText("Cost1");
		txtCost.setBounds(250, 92, 114, 19);
		add(txtCost);
		txtCost.setColumns(10);
		
		txtCost_1 = new JTextField();
//		txtCost_1.setText("Cost2");
		txtCost_1.setBounds(250, 118, 114, 19);
		add(txtCost_1);
		txtCost_1.setColumns(10);
		
		txtCost_2 = new JTextField();
//		txtCost_2.setText("Cost3");
		txtCost_2.setBounds(250, 147, 114, 19);
		add(txtCost_2);
		txtCost_2.setColumns(10);
		
		txtCost_3 = new JTextField();
//		txtCost_3.setText("Cost4");
		txtCost_3.setBounds(250, 178, 114, 19);
		add(txtCost_3);
		txtCost_3.setColumns(10);
		
		txtCost_4 = new JTextField();
//		txtCost_4.setText("Cost5");
		txtCost_4.setBounds(250, 209, 114, 19);
		add(txtCost_4);
		txtCost_4.setColumns(10);

	}
	
	public void display()
	{
		GUI_Main.replacePanel(this);
	}
	
	private class AddListener implements ActionListener{
		public void actionPerformed(ActionEvent event)
		{
			Invoice invoice = new Invoice();
			String cost;
			
			try
			{
			if(txtService.getText().equals("Service1")||txtService.getText()==null)
			{
				cost = txtCost.getText();
				Services service = new Services(txtService.getText(), Float.parseFloat(cost));
				invoice.addService(service);
				
			}
			if(txtService_1.getText().equals("Service2")||txtService_1.getText()==null)
			{
				cost = txtCost_1.getText();
				Services service = new Services(txtService_1.getText(), Float.parseFloat(cost));
				invoice.addService(service);
				
			}
			if(txtService_2.getText().equals("Service3")||txtService_2.getText()==null)
			{
				cost = txtCost_2.getText();
				Services service = new Services(txtService_2.getText(), Float.parseFloat(cost));
				invoice.addService(service);
				
			}
			if(txtService_3.getText().equals("Service4")||txtService_3.getText()==null)
			{
				cost = txtCost_3.getText();
				Services service = new Services(txtService_3.getText(), Float.parseFloat(cost));
				invoice.addService(service);
				
			}
			if(txtService_4.getText().equals("Service5")||txtService_4.getText()==null)
			{
				cost = txtCost_4.getText();
				Services service = new Services(txtService_4.getText(), Float.parseFloat(cost));
				invoice.addService(service);
				
			}
			} // try loop
			catch(Exception e)
			{
				JOptionPane.showMessageDialog(null, "Human: You have not entered the data properly\nProgrammer:" + e.toString());
			}
			
			GUI_Main.tscreen.setInvoice(invoice);
			System.out.println(invoice.toString());
			GUI_Main.tscreen.display();
	//		GUI_Main.getSession().getMapper(.class)
		}
	}
}
